<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        @click="isEdit = !isEdit"
        class="edit-btn"
        size="small"
        round
        color="red"
        plain
        type="primary"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" icon-size="0.4rem" :gutter="10" :border="false">
      <van-grid-item
        v-for="(item, index) in channels"
        :class="{ active: index === active }"
        :key="item.id"
        :icon="isEdit && index != 0 ? 'close' : ''"
        :text="item.name"
        @click="onClick(index,item.id)"
      />
    </van-grid>
    <van-cell title="推荐频道" :border="false"> </van-cell>
    <van-grid
      direction="horizontal"
      icon-size="0.4rem"
      :gutter="10"
      :border="false"
    >
      <van-grid-item
        @click="onLave(item)"
        v-for="item in laveChannel"
        :key="item.id"
        icon="plus"
        :text="item.name"
      />
    </van-grid>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage.js'
import { getAllChannels, addUserChannels, delUserChannels } from '@/api/news.js'
import { Toast } from 'vant'
export default {
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number
    }
  },
  data () {
    return {
      isEdit: false,
      allChannels: []
    }
  },

  computed: {
    ...mapState(['user']),
    laveChannel () {
      return this.allChannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
      // 对allChannels进行筛选
      // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
      // const arr = this.allChannels.filter(item => {
      //   const flag = this.channels.some(item2 => {
      //     return item2.id === item.id
      //   })
      //   return !flag
      // })
      // return arr
      // return this.allChannels.filter(item => {
      //   return !this.channels.some(item2 => item2.id === item.id)
      // })
    }
  },

  created () {
    this.loadAllChannels()
  },

  methods: {
    onClick (index, id) {
      if (this.isEdit && index !== 0) {
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channels.splice(index, 1)
        this.delChannels(id)
      } else {
        this.$emit('onGo', index)
      }
    },
    async delChannels (id) {
      Toast.loading({
        message: '删除中...',
        forbidClick: true,
        duration: 0
      })
      if (!this.user) {
        setStorage('MY_CHANNEL', this.channels)
        Toast.success('删除成功')
      } else {
        try {
          await delUserChannels(id)
          Toast.success('删除成功')
        } catch (err) {
          Toast.fail('删除失败')
        }
      }
    },
    async onLave (obj) {
      this.channels.push(obj)
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      if (this.user) {
        try {
          await addUserChannels({
            id: obj.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY_CHANNEL', this.channels)
        Toast.success('添加成功')
      }
    },
    async loadAllChannels () {
      try {
        const res = await getAllChannels()
        this.allChannels = res.channels
      } catch (err) {
        Toast.fail('获取全部频道失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }
  /deep/.van-grid-item {
    width: 160px;
    height: 86px;
    border-radius: 6px;
    .van-grid-item__content {
      background: #f4f5f6;
    }
  }
  .my-grid {
    /deep/.van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 0;
    }
    .active {
      /deep/.van-grid-item__text {
        color: red;
      }
    }
  }
}
</style>
